#include <stdio.h>
#include <unistd.h>
#include "mpi.h"

/**********************************************************
 * \file modified_sendrcvtest.c
 * \author Harry Ticer, James Edmondson
 * \brief Sends 2 MPI_CHAR, MPI_INT, MPI_FLOAT, and MPI_DOUBLE
 *        from non-zero ranks to the zero rank of MPI_COMM_WORLD.
 **********************************************************/

int
main(int argc, char **argv)
{
	int size, rank, i;

	int myint[2] = {4,8};
	char mychar[2] = {'A', 'Z'};
	float myfloat[2] = {0.2, 0.123456};
	double mydouble[2] = {0.2, 0.123456};
	
	int success = 1;
	
	MPI_Init(&argc, &argv);
	
	MPI_Comm_size(MPI_COMM_WORLD, &size);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);

	printf("Size %d, Rank %d\n",size,rank);
	
	if( rank == 0 )
	{

		for( i = 1; i < size; i++ )
		{
			MPI_Recv(myint,2,MPI_INT,MPI_ANY_SOURCE,10,MPI_COMM_WORLD,
					MPI_STATUS_IGNORE);
			MPI_Recv(mychar,2,MPI_CHAR,MPI_ANY_SOURCE,11,MPI_COMM_WORLD,
					MPI_STATUS_IGNORE);
			MPI_Recv(myfloat,2,MPI_FLOAT,MPI_ANY_SOURCE,12,MPI_COMM_WORLD,
					MPI_STATUS_IGNORE);
			MPI_Recv(mydouble,2,MPI_DOUBLE,MPI_ANY_SOURCE,13,MPI_COMM_WORLD,
					MPI_STATUS_IGNORE);
			printf("iRECV from %d: myint[0]: %d myint[1]: %d\n",
					i,myint[0], myint[1]);
			printf("cRECV from %d: mychar[0]: %c mychar[1]: %c\n",
					i,mychar[0], mychar[1]);
			printf("fRECV from %d: myfloat[0]: %f myfloat[1]: %f\n",
					i,myfloat[0], myfloat[1]);
			printf("dRECV from %d: mydouble[0]: %lf mydouble[1]: %lf\n",
					i,mydouble[0], mydouble[1]);
			success = success && (myint[0] == 4);
			success = success && (myint[1] == 8);
			success = success && (mychar[0] == 'A');
			success = success && (mychar[1] == 'Z');
			success = success && (myfloat[0] == (float)0.2);
			success = success && (myfloat[1] == (float)0.123456);
			success = success && (mydouble[0] == (double)0.2);
			success = success && (mydouble[1] == (double)0.123456);
		}
		if (success == 1) {
			printf("SUCCESS\n");
		}
	}
	else 
	{
		MPI_Send(myint,2,MPI_INT,0,10,MPI_COMM_WORLD);
		MPI_Send(mychar,2,MPI_CHAR,0,11,MPI_COMM_WORLD);
		MPI_Send(myfloat,2,MPI_FLOAT,0,12,MPI_COMM_WORLD);
		MPI_Send(mydouble,2,MPI_DOUBLE,0,13,MPI_COMM_WORLD);
		printf("iSEND: myint[0]: %d myint[1]: %d\n", myint[0], myint[1]);
		printf("cSEND: mychar[0]: %c mychar[1]: %c\n", mychar[0], mychar[1]);
		printf("fSEND: myfloat[0]: %f myfloat[1]: %f\n", myfloat[0], myfloat[1]);
		printf("dSEND: mydouble[0]: %lf mydouble[1]: %lf\n", mydouble[0], mydouble[1]);
	}

	MPI_Finalize();
	return 0;
}

